package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.DrugInformationMapper;
import com.drug.mapper.DrugQualityLevelMapper;
import com.drug.mapper.SdiDiInfMapper;
import com.drug.service.CommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private SdiDiInfMapper sdiDiInfMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private DrugQualityLevelMapper drugQualityLevelMapper;

    /**
     * 添加供货商和药品信息
     * @param
     * @return
     */
    @Override
    public Result addSdi(String[] sdiDi,Integer surId) {
        Result result = new Result();
        Integer i=0;
        Integer a=0;
        for (String s : sdiDi) {
            SdiDiInf sdiDiInf = new SdiDiInf();
            sdiDiInf.setSdiSId(surId);
            sdiDiInf.setSdiDiId(Integer.parseInt(s));
            i = sdiDiInfMapper.insertSdi(sdiDiInf);
            i=drugInformationMapper.updateSdiMent(s);
           a++;
        }
        if (i==1){
            result.setMsg("添加"+a+"条,成功"+a+"条");
        }
    return result;
    }

    @Override
    public Result addSdi(SdiDiInf sdiDiInf) {
        Integer  i = sdiDiInfMapper.insertSdi(sdiDiInf);
        Result result = new Result();
        int b=0;
        if (i==1){
            Integer  ii1 = drugInformationMapper.updateSdiId(sdiDiInf.getSdiDiId());
            if (ii1==1){
                result.setMsg("新增成功");

            }
        }else {
            result.setMsg("新增失败");
            b++;
        }
        result.setData(b);
        return result;
    }

    /**
     * 查询未供货的信息
     * @param map
     * @param pageNum
     * @return
     */
    @Override
    public Result findAllDCSdi(Map<String, Object> map, Integer pageNum) {
        //使用分页插件
        PageHelper.startPage(pageNum,5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //药品质量层次
        DrugQualityLevel drugQualityLevel = new DrugQualityLevel();

        String number = (String) map.get("dinumber");
        if (number==null||number.equals("")){
            number=null;
        }
        String diName = (String) map.get("cname");
        if (diName==null||diName.equals("")){
            diName=null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm==null||diForm.equals("")){
            diForm=null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications==null||diSpecifications.equals("")){
            diSpecifications=null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit==null||diUnit.equals("")){
            diUnit=null;
        }
        String diCc = (String) map.get("cc");
        if (diCc==null||diCc.equals("")){
            diCc=null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye==null||qiye.equals("")){
            qiye=null;
        }else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin==null||shangpin.equals("")){
            shangpin=null;
        }else {
            drugAtegory.setDaName(shangpin);
        }

        String lefPprice = (String) map.get("lefPprice");
        if (lefPprice==null||lefPprice.equals("")){
            lefPprice=null;
        }
        String rightPrice = (String) map.get("rightPrice");
        if (rightPrice==null||rightPrice.equals("")){
            rightPrice=null;
        }
        //质量层次
        String cengci = (String) map.get("cengci");
        if (cengci==null||cengci.equals("")||cengci.equals("0")){
            cengci=null;
        }else {
            drugQualityLevel.setDqlId(Integer.parseInt(cengci));
        }
        String leibie = (String) map.get("leibie");
        if (leibie==null||leibie.equals("")||leibie.equals("0")){
            leibie=null;
        }else {
            drugCategory.setDcName(leibie);
        }
        String state = (String) map.get("state");
        if (state==null||state.equals("")){
            state=null;
        }
        //药品信息
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(number);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiTradingStatus(state);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        drugInformation.setDrugQualityLevel(drugQualityLevel);

        //多条件查询
        List<SdiDiInf> sdiDiInfs = sdiDiInfMapper.selectDISdi(drugInformation,lefPprice,rightPrice);
        PageInfo<SdiDiInf> pageInfo= new PageInfo<SdiDiInf>(sdiDiInfs);
        result.setData(pageInfo);
        return result;

    }

    /**
     * 取消供货
     * @param sdiId
     * @return
     */
    @Override
    public Result updaueSdi(String[] sdiId) {
        Result result = new Result();
        Integer integer=0;
        if (sdiId!=null){
            for (String sdi : sdiId) {
                integer = sdiDiInfMapper.updateSdi(Integer.parseInt(sdi));
                Integer integer1 = sdiDiInfMapper.selectDiId(Integer.parseInt(sdi));
                drugInformationMapper.updateMent(integer1);
                integer++;
            }
            if (integer>=1){
                result.setMsg("取消成功");
            }else {
                result.setMsg("取消失败");
            }
        }else {
            result.setMsg("请勾选");
        }
        return result;
    }

    /**
     * 修改
     * @param drugInformation
     * @return
     */
    @Override
    @Transactional
    public Result updateDif(DrugInformation drugInformation) {
        int i = drugInformationMapper.updateDif(drugInformation);
        String diDqlName = drugInformation.getDiDqlName();
        int s = drugQualityLevelMapper.selectIdByName(diDqlName);
        drugInformation.setDiDqlName(s+"");
        Result result = new Result();
        if (i==1){
            result.setMsg("修改成功");
        }else {
            result.setMsg("修改失败");
        }
        return result;
    }

}
